<template>
	<view class="">
		<view class="scroll_header"></view>
		<view class="mt-5" v-if="u_loadmore">
			<u-loadmore status="loading" icon-type="circle" />
		</view>
		<view v-else>
			<scroll-view scroll-y="true" :style="{height: scrollHeight}">
					<view class="m-2 bg-white rounded-20">
						<view class="d-flex flex-column j-center a-center py-2">
							<view class="position-relative">
								<image class="editImg rounded-20" :src="editList.img.url?editList.img.url:defaltImage" mode="" @click="basic_edit"></image>
								<view class="position-absolute top-0 left-0 z-222" v-if="editList.goods_video_url">
									<image src="../../static/images/goods/video.png" mode="aspectFit" class="video_img"></image>
								</view>
							</view>
							<view v-if="editList.name.length>15" class="">
								<view class="d-flex a-center j-center py-2">
									<view class="p bg-danger mr-2 text-white rounded-10">{{editList.is_on_sale?'已上架':'已下架'}}</view>
									<image v-if="editList.is_promote" src="../../static/images/goods/promotion.png" class="mr chu_edit" mode=""></image>
								</view>
								<view class="font-md mx-2">{{editList.name}}</view>
							</view>
							<view v-else class="d-flex a-center j-center py-2">
								<view class="p bg-danger mr-2 text-white rounded-10">{{editList.is_on_sale?'已上架':'已下架'}}</view>
								<image v-if="editList.is_promote" src="../../static/images/goods/promotion.png" class="mr chu_edit" mode=""></image>
								<view class="font-md ellipsis">{{editList.name}}</view>
							</view>
							<view class="pb-2 text-danger font-lg">{{editList.shop_price}}</view>
							<view class="d-flex">
								<view class="store mr-2">库存:&nbsp;&nbsp;{{editList.stock}}</view><view class="store">销量:&nbsp;&nbsp;{{editList.sales_volume}}</view>
							</view>
							<view class="store mt-2">商品分类:&nbsp;&nbsp;{{editList.category_name}}</view>
							<view class="store mt-2 ellipsis-1 mx-3" v-if="editList.merchant_category_name">店铺商品分类:&nbsp;&nbsp;{{editList.merchant_category_name}}</view>
						</view>
					</view>
					<!-- 店铺推荐 -->
					<!-- <view class="m-2 bg-white rounded-20 py-2 pl-3">
						<view class="font-md">店铺推荐</view>
						<view class="py-3 d-flex">
							<view class="flex-1">
								<u-checkbox-group  @change="cakes">
									<u-checkbox v-model="editList.store_hot" shape="circle" icon-size="30"  size="40" active-color="#F24F4A"
									>热销</u-checkbox>
								</u-checkbox-group>
							</view>
							<view class="flex-1">
								<u-checkbox-group @change="prod">
									<u-checkbox  v-model="editList.store_new"  shape="circle" icon-size="30"  size="40" active-color="#F24F4A"
									>新品</u-checkbox>
								</u-checkbox-group>
							</view>
							<view class="flex-1">
								<u-checkbox-group  @change="products">
									<u-checkbox v-model="editList.store_best"  shape="circle" icon-size="30"  size="40" active-color="#F24F4A"
									>精品</u-checkbox>
								</u-checkbox-group>
							</view>
						</view>
					</view> -->
					
					
					
					
					<!-- <view class="rounded-20 m-2 pl-3  bg-white clazz" >
						<view class="d-flex j-sb pr-2 py-3 a-center clazz" @click="goodsSpec">
							<view class="">商品规格</view>
							<view class="d-flex j-center a-center">
								<text class="text-light-muted mr-1">去设置</text>
								<u-icon name="arrow-right" color="#909399" size="30"></u-icon>
							</view>
						</view>			
					</view> -->
					
					<view class="rounded-20 m-2 pl-3  bg-white " >
						<view class="d-flex j-sb border-bottom pr-2 py-3 a-center clazz" @click="goodsAlbum">
							<view class="">商品相册</view>
							<view class="d-flex j-center a-center">
								<text class="text-light-muted mr-1">去编辑</text>
								<u-icon name="arrow-right" color="#909399" size="30"></u-icon>
							</view>
						</view>
						<view class="d-flex j-sb pr-2 py-3 border-bottom a-center clazz" @click="goods_video">
							<view class="">商品视频</view>
							<view class="d-flex j-center a-center">
								<text class="text-light-muted mr-1">去编辑</text>
								<u-icon name="arrow-right" color="#909399" size="30"></u-icon>
							</view>
						</view>
						<view class="d-flex j-sb pr-2 py-3 a-center clazz" @click="goods_desc">
							<view class="">图文详情</view>
							<view class="d-flex j-center a-center">
								<text class="text-light-muted mr-1">去编辑</text>
								<u-icon name="arrow-right" color="#909399" size="30"></u-icon>
							</view>
						</view>			
					</view>
					<view class="rounded-20 m-2 pl-3  bg-white">
						<view class="d-flex j-sb pr-2 py-3 a-center clazz" @click="goodsPrice">
							<view class="">商品价格</view>
							<view class="d-flex j-center a-center">
								<text class="text-light-muted mr-1">去编辑</text>
								<u-icon name="arrow-right" color="#909399" size="30"></u-icon>
							</view>
						</view>
						<!-- <view class="d-flex j-sb pr-2 py-3 a-center clazz" @click="trademark">
							<view class="">商品品牌</view>
							<view class="d-flex j-center a-center">
								<text class="text-light-muted mr-1">{{brand_item?brand_item:"去设置"}}</text>
								<u-icon name="arrow-right" color="#909399" size="30"></u-icon>
							</view>
						</view>	 -->		
					</view>
					<view class="rounded-20 m-2 pl-3 bg-white clazz" >
						<view class="d-flex j-sb pr-2 py-3 a-center clazz" @click="goodsAss">
							<view class="">关联商品</view>
							<view class="d-flex j-center a-center">
								<text class="text-light-muted mr-1">{{editList.has_linked_goods=='yes'?'已关联':'去关联'}}</text>
								<u-icon name="arrow-right" color="#909399" size="30"></u-icon>
							</view>
						</view>			
					</view>
					
					<!-- 商家服务 -->
					<view class="m-2 bg-white rounded-20 py-2 pl-3">
						<view class="font-md">商品服务</view>
						<view class="py-3 d-flex ">
							<view class="flex-1">
								<u-checkbox-group>
									<u-checkbox v-model="editList.is_return"  shape="circle" icon-size="30"  size="40" active-color="#F24F4A"
									@change="service">包退服务</u-checkbox>
								</u-checkbox-group>
							</view>
							<view class="flex-1">
								<u-checkbox-group>
									<u-checkbox v-model="editList.is_reality"  shape="circle" icon-size="30"  size="40" active-color="#F24F4A"
									@change="product">正品保证</u-checkbox>
								</u-checkbox-group>
							</view>
							<view class="flex-1">
								<u-checkbox-group>
									<u-checkbox  v-model="editList.is_fast" shape="circle" icon-size="30"  size="40" active-color="#F24F4A"
									@change="dispatching">闪送配送</u-checkbox>
								</u-checkbox-group>
							</view>
						</view>
					</view>
					<view class="rounded-20 m-2 pl-3 bg-white" >
						<view class="d-flex j-sb pr-2 py-3 a-center clazz border-bottom">
							<view class="">显示</view>
							<view class="d-flex j-center a-center">
								<u-switch @change="added" v-model="editList.is_on_sale" size="43"></u-switch>
							</view>
						</view>
						<view class="d-flex j-sb pr-2 py-3 a-center clazz border-bottom">
							<view class="">是否包邮</view>
							<view class="d-flex j-center a-center">
								<u-switch @change="shipping" v-model="editList.is_shipping" size="43"></u-switch>
							</view>
						</view>
						<view class=" a-center clazz border-bottom d-flex j-sb " v-show="!editList.is_shipping" @click="freight">
							<view class="">商品运费</view>
							<view class="d-flex j-center a-center mr-2">
								<text class="text-light-muted mr-2">{{freight_price?'固定模板['+freight_price+']':'去设置'}}</text>
								<image src="../../static/images/user/icon_right_arrow.png" mode="" class="icon_right_arrow"></image>
							</view>	
						</view>
						<view class="d-flex j-sb pr-2 py-3 a-center clazz ">
							<view class="">普通商品</view>
							<view class="d-flex j-center a-center">
								<u-switch @change="ordinary" v-model="editList.is_alone_sale" size="43"></u-switch>
							</view>
						</view>			
					</view>
					<view class="py-1 text-center font-sm">
						开启则此为商品作为普通商品销售,关闭则此商品只能作为赠品或配件销售
					</view>
					<u-toast ref="uToast"/>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				defaltImage:'../../static/images/goods/icon_default_image.png',
				editList:'', // 详情数据 
				goods_id:'' ,// 商品id标识,
				service_show:false, // 包退服务
				product_show:false, // 正品保证
				dispatching_show:false, // 闪送配送
				brand_item:"", // 商品品牌
				scrollHeight:0, // 自适应高度
				u_loadmore:true, // 加载中
				freight_price:"", // 运费
			}
		},
		onReady() {
			let _this=this
			uni.getSystemInfo({
				success(resu) {
					const query = uni.createSelectorQuery()
					query.select('.scroll_header').boundingClientRect()
					query.exec(function(res) {
						_this.scrollHeight = resu.windowHeight - res[0].bottom+ 'px';
					})
				}
			})
		},
		methods: {
			// 是否包邮
			shipping(){
				if(this.editList.is_shipping){
					this.$H.post("admin/goods/toggle/free_shipping",{id:this.goods_id,is_free:1}).then(res=>{
					})
				}else{
					this.$H.post("admin/goods/toggle/free_shipping",{id:this.goods_id,is_free:0}).then(res=>{
					})
				}
			},
			// 商品运费
			freight(){
				uni.navigateTo({
					url:"/pages/goods-freight/goods-freight"
				})
			},
			// 编辑商品基本信息
			basic_edit(){
				uni.navigateTo({
					url:"/pages/goods-edit-basic/goods-edit-basic?goods_id="+this.goods_id
				})
			},
			// 商品价格
			goodsPrice(){
				uni.navigateTo({
					url:'/pages/goods-edit-expert/goods-edit-expert?goods_id='+this.goods_id
				})
			},
			// 商品规格
			goodsSpec(){
				uni.navigateTo({
					// url:'/pages/goods-edit-spec/goods-edit-spec?goods_id='+this.goods_id
					url:'/pagesA/goods/spec-option-project?goods_id='+this.goods_id
				})
			},
			// 商品品牌
			trademark(){
				uni.navigateTo({
					url:'/pages/goods-trademark/goods-trademark?goods_id='+this.goods_id
				})
			},
			// 商品相册
			goodsAlbum(){
				uni.navigateTo({
					url:'/pages/goods-edit-album/goods-edit-album?goods_id='+this.goods_id
				})
			},
			// 商品视频
			goods_video(){
				if(this.editList.goods_video_url==''){
					uni.chooseVideo({
					    count: 1,
					    sourceType: ['album'],
						compressed:false,
					    success: (res)=>{
							uni.showLoading({
								title:"上传中",
								success: () => {
									let tempFilePath= res.tempFilePath;
									console.log(tempFilePath)
									uni.uploadFile({
										url:`${this.localConfig.web_Url}v2/store/medialibrary/file/temp/upload`,
										filePath:res.tempFilePath,
										name:"tempfile",
										formData:{
											'token':uni.getStorageSync("token"),
										},
										success: (res) => {
											let fileData=JSON.parse(res.data)
											this.$H.post("v2/store/goods/video/update",{
												goods_id:this.goods_id,
												goods_video:fileData.data
											}).then(res=>{
												if(res.status.succeed==0){
													this.$refs.uToast.show({
														title: '上传文件大小不符！',
														type: 'default',
													})
													uni.hideLoading()
												}else{
													uni.navigateTo({
														url:'/pages/goods-video/goods-video?goods_id='+this.goods_id
													})
													uni.hideLoading()
												}
											})
										}
									})
								}
							})
					    }          
					});
				}else{
					uni.navigateTo({
						url:'/pages/goods-video/goods-video?goods_id='+this.goods_id
					})
				}
			},
			// 关联商品
			goodsAss(){
				uni.setStorageSync("goods_id",JSON.stringify(this.goods_id))
				if(this.editList.has_linked_goods=="yes"){
					uni.navigateTo({
						url:`/pages/goods-ass-goods/goods-ass-goods?goods_id=${this.goods_id}&option=详情&ass_name=详情`
					})
				}else{
					uni.navigateTo({
						url:`/pages/goods-edit-ass/goods-edit-ass?goods_id=${this.goods_id}&option=选择&edit_name=选择`
					})
					console.log(22)
				}
				
			},
			// 图文描述
			goods_desc(){
				uni.navigateTo({
					url:'/pages/goods-edit-desc/goods-edit-desc?goods_id='+this.goods_id
				})
			},
			// 热销
			cakes(e){
				this.$H.post("admin/goods/toggle/suggest",{id:this.goods_id,type:'hot',is_suggest:this.editList.store_hot}).then(res=>{
					console.log(res,this.editList.store_hot)
				})
			},
			// 新品
			prod(e){
				this.$H.post("admin/goods/toggle/suggest",{id:this.goods_id,type:'new',is_suggest:this.editList.store_new}).then(res=>{
					console.log(res,this.editList.store_new)
				})
			},
			// 精品
			products(e){
				this.$H.post("admin/goods/toggle/suggest",{id:this.goods_id,type:'best',is_suggest:this.editList.store_best}).then(res=>{
					console.log(res,this.editList.store_best)
				})
			},
			
			// 包退服务
			service(e){
				this.$H.post("admin/merchant/goods/goodsservice/update",{goods_id:this.goods_id,type:'return',is_support:!this.editList.is_return}).then(res=>{
					console.log(res,this.editList.is_return)
				})
			},
			// 正品保证
			product(e){
				this.$H.post("admin/merchant/goods/goodsservice/update",{goods_id:this.goods_id,type:'real_guarantee',is_support:!this.editList.is_reality}).then(res=>{
					console.log(res,this.editList.is_reality)
				})
			},
			// 闪送配送
			dispatching(e){
				this.$H.post("admin/merchant/goods/goodsservice/update",{goods_id:this.goods_id,type:'fast_delivery',is_support:!this.editList.is_fast}).then(res=>{
					console.log(res,this.editList.is_fast)
				})
			},
			// 普通商品
			ordinary(){
				if(this.editList.is_alone_sale){
					this.$H.post("admin/goods/toggle/gifts",{id:this.goods_id,is_gift:0}).then(res=>{
						console.log(res)
					})
				}else{
					this.$H.post("admin/goods/toggle/gifts",{id:this.goods_id,is_gift:1}).then(res=>{
						console.log(res)
					})
				}
				
			},
			// 上下架商品
			added(){
				if(this.editList.is_on_sale){
					this.$H.post("admin/goods/toggle/sale",{id:this.goods_id,type:"online"}).then(res=>{
						console.log(res,this.editList.is_on_sale)
					})
				}else{
					this.$H.post("admin/goods/toggle/sale",{id:this.goods_id,type:"offline"}).then(res=>{
						console.log(res,this.editList.is_on_sale)
					})
				}
				
			}
		},
		onLoad(e) {
			this.goods_id=e.goods_id
			this.$H.post("admin/merchant/goods/detail",{goods_id:this.goods_id}).then(res=>{
				console.log(res)
				this.editList=res.data
				this.brand_item=this.editList.brand_name
				this.u_loadmore=false 
				this.freight_price=this.editList.formated_goods_shipping_fee
				// 运费
				if(!this.editList.is_shipping){
					let num = parseInt(this.editList.formated_goods_shipping_fee.replace("￥",'')).toFixed(1)
					this.freight_price= num
					var globalData = getApp().globalData.freight; //获取全局变量
					getApp().globalData.freight= num; //修改全局变量
				}
			})
		},
		onUnload() {
			var globalData = getApp().globalData.brand_item; //获取全局变量
			getApp().globalData.brand_item= null; //修改全局变量
			
			var globalData = getApp().globalData.freight; //获取全局变量
			getApp().globalData.freight= null; //修改全局变量
		},
		onShow() {
			this.$H.post("admin/merchant/goods/detail",{goods_id:this.goods_id}).then(res=>{
				this.editList=res.data
			})
			
			
			var globalData = getApp().globalData.clazz; //获取全局变量
			getApp().globalData.clazz= null; //修改全局变量
			
			var globalData = getApp().globalData.shopClazz; //获取全局变量
			getApp().globalData.shopClazz= null; //修改全局变量
			
			if(getApp().globalData.brand_item){
				let globalData=getApp().globalData.brand_item
				this.brand_item=globalData.brand_name
				console.log(this.brand_item)
			}
			if(getApp().globalData.freight){
				let globalData = getApp().globalData.freight;
				if(globalData){
					this.freight_price=globalData
					// 运费
					if(this.freight_price){
						let globalData = getApp().globalData.freight_template;
						console.log(globalData)
						this.$H.post("admin/goods/shipping/setting",{
							goods_id:this.goods_id,
							shipping_type:"fixed",
							shipping_fee:this.freight_price,
							template_id:globalData.template_id
						}).then(res=>{
							console.log(res)
							var globalData = getApp().globalData.freight; //获取全局变量
							getApp().globalData.freight= null; //修改全局变量
						})
					}
				}
			}
			
			
		}
	}
</script>

<style>
	.editImg{
		width: 400upx;
		height: 400upx;
	}
	.store{
		color: #F84B4F;
		padding: 9upx 20upx;
		background: linear-gradient(to right,#FEDFE4,#FFE7E3);
		border-radius: 50upx;
	}
	.deep /deep/ .u-switch{
		font-size: 40upx;
		border: #0000FF;
	}
	.deep /deep/ .u-switch__node{
		background-color: #000000;
		width: 40upx;
		height: 40upx;
	}
	.chu_edit{
		width: 50upx;
		height: 50upx;
	}
</style>
